(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Implementing a State Layer

Description: C++ Walkthrough

Tutorial Level: INTERMEDIATE

Next Tutorial: Implementing an Observation Layer

Note: If your decision-making problem is partially observable (POMDPs, Dec-POMDPs, etc.), then you don't need a state layer.

The following example demonstrates how a State Layer ROS node can be implemented in C++. There are two state factors in the problem: the first is an integer-valued state factor describing the topological location of a robot in its environment; the second is a binary variable representing whether or not there is a person waiting for assistance.

   1 #include <ros/ros.h>
   2 
   3 #include <markov_decision_making/StateLayer.h>
   4 
   5 using namespace ros;
   6 using namespace markov_decision_making;
   7 
   8 int main (int argc, char** argv)
   9 {
  10   init (argc, argv, "state_layer_example");
  11 
  12   StateLayer robot_mls;
  13   robot_mls.addStateFactor (StateDep()
  14                             .add ("IsInElevatorHallway")
  15                             .add ("IsInWestCorridor")
  16                             .add ("IsInSouthCorridor")
  17                             .add ("IsInCoffeeRoom")
  18                             .add ("IsInLRM")
  19                             .add ("IsInSoccerField"));
  20   robot_mls.addStateFactor (StateDep()
  21                             .add ("PersonIsWaiting"));
  22   spin();
  23 
  24   return 0;
  25 }

We will now analyze the code (besides the bare minimum ROS node initialization / support):

   1 StateLayer robot_mls;

This declares our State Layer, which will be silently connecting to Predicate Manager topics (~/predicate_map and ~/predicate_updates), but will not be spinning its own thread. Note that the spin() function is still handled externally, and it is only called after state factor variables are declared. The State Layer will be publishing its state information to the ~/state topic.

   1 robot_mls.addStateFactor (StateDep()
   2                           .add ("IsInElevatorHallway")
   3                           .add ("IsInWestCorridor")
   4                           .add ("IsInSouthCorridor")
   5                           .add ("IsInCoffeeRoom")
   6                           .add ("IsInLRM")
   7                           .add ("IsInSoccerField"));

This adds an integer state factor to the State Layer, and binds its value to a set of mutually exclusive predicates which describe whether or not the robot is in a particular topological position. State factors must be added in the order that the user wants them to appear in the factored state description - that is, this code snippet will be considered as state factor X1. Likewise, predicates are added as dependencies in the order that they should be assigned to state factor values -- "IsInElevatorHallway" will correspond to the first value of this state factor. The StateDep() class is used to easily register a chain of predicates as dependencies. Note that, following the C++ standard and to maintain consistency with the internal operation of MDM, all indexes start at 0. This means that the domain of this state factor is X1 = {0,...,5}.

   1 robot_mls.addStateFactor (StateDep()
   2                             .add ("PersonIsWaiting"));

This binds the second state factor to the predicate "PersonIsWaiting", which means that x2 = 1 iff the predicate is true.

   1 spin();

This spins this node's thread in a loop, during which the State Layer will be running and listening for predicate updates. Currently, the state description cannot be changed after the spin() function is called. If the state description contained in a State Layer does not match what is expected by an associated Control Layer, a warning will be thrown.

Wiki: markov_decision_making/Tutorials/Implementing a State Layer (last edited 2015-07-16 12:22:28 by JoaoMessias)